﻿Imports System.IO
Imports System.Windows
Imports System.Windows.Controls
Imports DocumentToolkit.Client
Imports Microsoft.LightSwitch.Threading

Namespace LightSwitchApplication.UserCode


    Partial Public Class SelectFileWindow
        Inherits ChildWindow


        Public Sub New()
            InitializeComponent()
            Me._Filter = "All Files (*.*)|*.*"
            Me._DoIndexFilter = 1
        End Sub

        Public Sub New(Filter As String, DoIndexFilter As Integer)
            InitializeComponent()
            Me._Filter = Filter
            Me._DoIndexFilter = DoIndexFilter
        End Sub

        Private _Filter As String
        Private _DoIndexFilter As Integer
        'Private _NomeDoArquivo As String

        Private m_documentStream As FileStream

        Public Property DocumentStream() As FileStream
            Get
                Return m_documentStream
            End Get

            Set(value As FileStream)
                m_documentStream = value
            End Set
        End Property

        Private m_safeFileName As [String]

        Public Property SafeFileName() As [String]
            Get
                Return m_safeFileName
            End Get

            Set(value As [String])
                m_safeFileName = value
            End Set
        End Property

        Private _Fd As Byte()
        Public ReadOnly Property Fd() As Byte()
            Get
                Return _Fd
            End Get
        End Property

        Private _FileInfo As FileInfo()
        Public Property FI() As FileInfo()
            Get
                Return _FileInfo
            End Get
            Set(value As FileInfo())
                _FileInfo = value
            End Set
        End Property

        Private Sub OKButton_Click(sender As Object, e As RoutedEventArgs)

            Me.DialogResult = True

        End Sub


        Private Sub CancelButton_Click(sender As Object, e As RoutedEventArgs)

            Me.DialogResult = False

        End Sub

        Private Sub BrowseButton_Click(sender As Object, e As RoutedEventArgs)
            Dim result As FileInfo = Nothing

            Dim openFileDialog As New OpenFileDialog()
            openFileDialog.Filter = Me._Filter
            openFileDialog.FilterIndex = Me._DoIndexFilter

            If openFileDialog.ShowDialog() = True Then
                result = openFileDialog.File
                'Me.FI = openFileDialog.Files

                Me.FileTextBox.Text = openFileDialog.File.Name

                Me.m_safeFileName = openFileDialog.File.Name

                Me.FileTextBox.IsReadOnly = True

                Dim myStream As FileStream = openFileDialog.File.OpenRead()

                Me.m_documentStream = myStream
                'retornando um Array de bytes
                Using Sr As New StreamReader(myStream)
                    Dim FileData As Byte() = New Byte(DocumentStream.Length - 1) {}
                    For i As Long = 0 To DocumentStream.Length - 1
                        FileData(i) = CByte(DocumentStream.ReadByte())
                    Next
                    Me._Fd = FileData
                End Using
            End If

        End Sub

        Public Function SelectFile()
            Dim result As FileInfo = Nothing
            Dispatchers.Main.Invoke(
            Sub()
                Dim dlg = New OpenFileDialog()
                If dlg.ShowDialog = True Then
                    result = dlg.File
                End If
            End Sub)
            SelectFile = result
        End Function

    End Class

End Namespace
